//
//  TwoButtonsPopViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/4.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "TwoButtonsPopViewController.h"

#import "HttpServer.h"
#import "ACMacros.h"
#import "Utility.h"
#import "Constants.h"
#import "User.h"

#import <TSMessages/TSMessageView.h>


@interface TwoButtonsPopViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@end

@implementation TwoButtonsPopViewController {
    UIImagePickerController *photoPicker;
    User *user;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    user = [User sharedUser];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.navigationController.navigationBarHidden = true;
}

- (void)viewWillDisappear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    self.navigationController.navigationBarHidden = false;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Triggering Methods
- (IBAction)clickToCancelPop:(UIButton *)sender {
    [self dismissViewControllerAnimated:true completion:^{
        
    }];
}
- (IBAction)firstButtonClicked:(UIButton *)sender {
    if ([self.sourceView isEqual:@"Avatar"]) {
        [self clickToPickImageFromCamera];
    } else if ([self.sourceView isEqual:@"Gender"]) {
        [self clickToChooseMale];
    } else if ([self.sourceView isEqual:@"Share"]) {
        [self clickToChooseShareMethodWechat];
    }

}

- (IBAction)secondButtonClicked:(UIButton *)sender {
    if ([self.sourceView isEqual:@"Avatar"]) {
        [self clickToPickImageFromPhotos];
    } else if ([self.sourceView isEqual:@"Gender"]) {
        [self clickToChooseFemale];
    } else if ([self.sourceView isEqual:@"Share"]) {
        [self clickToChooseShareMethodTimeLine];
    }
}

- (void)clickToPickImageFromCamera {
    photoPicker = [[UIImagePickerController alloc] init];
    photoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    photoPicker.delegate = self;
    photoPicker.allowsEditing = YES;
    [self presentViewController:photoPicker animated:YES completion:^{
    }];

}

- (void)clickToPickImageFromPhotos {
    photoPicker = [[UIImagePickerController alloc] init];
    photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    photoPicker.delegate = self;
    photoPicker.allowsEditing = YES;
    [self presentViewController:photoPicker animated:YES completion:^{
    }];
}

- (void)clickToChooseMale {
    user.gender = @"m";
    [self dismissViewControllerAnimated:true completion:^{
        
    }];
}

- (void)clickToChooseFemale {
    user.gender = @"f";
    [self dismissViewControllerAnimated:true completion:^{
        
    }];
}

- (void)clickToChooseShareMethodWechat {
    [self.delegate sharebuttonClickedWithTag:0];
}

- (void)clickToChooseShareMethodTimeLine {
    [self.delegate sharebuttonClickedWithTag:1];
}

#pragma mark - Image picker view delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0) {
    [photoPicker dismissViewControllerAnimated:true completion:^{
    }];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.3);
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"avatarFile.png"];
    [imageData writeToFile:fullPath atomically:false];
    [HttpServer postAvatarImageWith:imageData WithSuccessBlock:^(NSDictionary *successDic) {
        user = [User sharedUser];
        user.avatar = [[successDic objectForKey:@"resource"] objectForKey:@"uri"];
        UIImage *avatarImage = [UIImage imageWithData:imageData];
        [Utility setObject:user key:kUser];
        [Utility setObject:avatarImage key:kAvatar];
        [self dismissViewControllerAnimated:true completion:^{
        }];
    } withFailBlock:^(id failData) {
        NSLog(@"%@",failData);
        [TSMessage showNotificationInViewController:self title:@"上传头像失败!" subtitle:@"请稍后重新上传头像" type:TSMessageNotificationTypeError duration:2.0 canBeDismissedByUser:true];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [photoPicker dismissViewControllerAnimated:true completion:^{
    }];
}
@end
